package com.twu.biblioteca.model.users;

import com.twu.biblioteca.model.users.exceptions.UserValidationException;

import java.util.Arrays;
import java.util.HashMap;

/**
 * Created with IntelliJ IDEA.
 * User: cschlumb
 * Date: 09/08/2013
 * Time: 14:16
 * To change this template use File | Settings | File Templates.
 */
public class UserManager {

    private final HashMap<Username, User> userList = new HashMap<Username, User>();

    public void add(User user) {
        userList.put(user.getUsername(), user);
    }

    public int size() {
        return userList.size();
    }

    public User get(Username username){
        return userList.get(username);
    }

    public void authenticateUser(Username username, String password) throws UserValidationException {
        if (! userList.containsKey(username))
            throw new UserValidationException();

        User user = userList.get(username);

        if (! Arrays.equals(user.getHashedPassword(), SecurityUtils.hashPassword(password, user.getPasswordSalt())))
            throw new UserValidationException();
    }
}
